var list = new Array();

function showHideShoppingList(){
	if (document.getElementById('list').value == 'shopping list'){
		document.getElementById('list').value = 'hide list';
		setEditModeDone();
		document.getElementById("shopping-list-overlay").style.display = 'block';
		agent.call('','refresh','callListRefresh');
	} else {
		hideWaiter(null);
		document.getElementById('list').value = 'shopping list';
		editmode = 0;
		agent.call('','refresh','up_callback');
		$("#shopping-list").css("z-index","1000"); 
		document.getElementById("content_list").innerHTML = '';
	}	
}

function addToShoppingList(id){
	document.getElementById("shopping-list-overlay").style.display = 'block';
	agent.call('', 'addToSList', 'refresh', id);
}

function hideWaiter(result){
	document.getElementById("shopping-list-overlay").style.display = 'none';
}

function refresh(result){
	list = result;
	hideWaiter(result);
	generateList();
	 $('.edit').editable('save.php',{
		 id   : 'elementid',
         name : 'newvalue',
         indicator : '<img src="img/indicator.gif">',
         submit    : 'OK',
         tooltip   : 'Цък за да редактираш...'
	 });
}

function contains(pname){
	for(i=0; i<list.length;i++){
		if (list[i][0] == pname){
			return i;
		}
	}
	return -1;
}

function removeElement(id){
	document.getElementById("shopping-list-overlay").style.display = 'block';
	agent.call('', 'removeFromList', 'refresh', id);
}

function getFullList(){
	var result = '';
	if (list.length > 0){
		result += generateListHtml(true);
	} else {
		result += '<p>Списъкът е празен.</p>';
	}
	return result;
}

function generateList(){
	var result = getFullList();
	document.getElementById("content_list").innerHTML = result;
}

function generateListHtml(includeRemove){
	var result = '';
	if (list.length > 0) {
		for(i=0; i<list.length;i++){
			result +=generateCheckComponent(list[i][0], list[i][2], list[i][1], list[i][3], includeRemove);
		}
	}
	return result;
}



function generateMailList(){
	return '<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
			+ '<title>Grocaries</title></head><body>'
			+generateListHtml(false)
			+'<script type="text/javascript">'
			+ 'function strikeText(el, doIt) {if (typeof el == \'string\') el = document.getElementById(el); el.style.textDecoration = (doIt)? \'line-through\':\'\';}'
			+'</script>'
			+'</body></html>';
}

function emailList(){
	var html = generateMailList();
	agent.call('','mailList','mailSent', html);
}

function emailGroupList(){
	var html = generateMailList();
	agent.call('','mailGroupList','mailSent', html);
}

function clearList(){
	agent.call('','clearList','refresh');
}

function mailSent(result){
	alert('Email was sent to: ' +result);
}

function saveList(){}

function initSlist(){
	 $(".editable_textarea").editable('save.php');
}

function generateCheckComponent(id, brand, name, q, includeRemove){
	var remove = ''; 
	if (includeRemove){
		remove = ' <a href="#" onclick="removeElement(\''+id+'\');"> <img src="images/20pxRedX.png"> </img> </a>';
	}
	return '<span id="e'+id+'"> <input type="checkbox" onclick="strikeText(\''+id+'\', this.checked);"> '+name +' '+ brand +'  '+remove+' </input></span> '+ 
			'<div class="edit" id="'+id+'">'+q+ '</div><br> </br>';
}

function strikeText(el, doIt) {
	if (typeof el == 'string') el = document.getElementById('e'+el);
	el.style.textDecoration = (doIt)? 'line-through':'';
}

function initDragDrop(){
	$('.draggable').draggable();
	$('.droppable').droppable({
			drop: function(event, ui) {
				$(this).addClass('ui-state-highlight').find('p').html('Dropped!');
			}
		});
}

function toodledoSync(){
	document.getElementById("shopping-list-overlay").style.display = 'block';
	agent.call('', 'toodledoSyncPhp', 'hideWaiter');
}